package com.okq.service.Impl;

import com.okq.dao.CustomerDao;
import com.okq.dao.JobDao;
import com.okq.entity.Customer;
import com.okq.entity.Job;
import com.okq.service.IJobService;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 描述 : 工单接口实现类
 * 作者 : zdl
 * 日期 : 2017/6/29 10:52
 */
@Transactional
@Service
public class JobServiceImpl implements IJobService {

    @Autowired
    private JobDao jobDao;

    @Autowired
    private CustomerDao customerDao;

    @Override
    public void saveJob(Job job) {
        jobDao.saveJob(job);
    }

    @Override
    public void deleteJob(Job job) {
        jobDao.deleteJob(job);
    }

    @Override
    public void updateJob(Job job) {
        jobDao.updateJob(job);
    }

    @Override
    public void saveOrUpdateJob(Job job) {
        jobDao.saveOrUpdateJob(job);
    }

    @Override
    public Job getJobById(String id) {
        return jobDao.getJobById(id);
    }

    @Override
    public List<Job> listAllJob() {
        return jobDao.listAllJob();
    }

    @Override
    @SuppressWarnings("all")
    public Page<Job> pageByJob(Page<Job> page, Job job) {
        if (job != null) {
            String customerKeyWord = job.getCustomerKeyWord();
            if (StringUtils.isNotBlank(customerKeyWord)) {
                List<Customer> cList = customerDao.listCustomerByKeyword(customerKeyWord);
                if (cList != null && !cList.isEmpty() && cList.size() > 0) {
                    String customerIds = "";
                    for (Customer c : cList) {
                        customerIds = c.getId() + ",";
                    }
                    if (StringUtils.isNotBlank(customerIds)) {
                        customerIds = customerIds.substring(0, (customerIds.length() - 1));
                        job.setCustomerIds(customerIds);
                    }
                }
            }
        }
        return jobDao.pageByJob(page, job);
    }

    @Override
    public Job getJobByCode(String code) {
        return jobDao.getJobByCode(code);
    }

    @Override
    public Job getLastJob() {
        return jobDao.getLastJob();
    }
}
